package com.zeratul.common.lock.config;

import com.zeratul.common.lock.aspect.ZtLockAspect;
import com.zeratul.common.lock.lock.base.BaseZtLock;
import com.zeratul.common.lock.lock.redisson.RedissonZtLock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;

@AutoConfiguration
@Slf4j
public class ZeratulLockAutoConfiguration {

    /**
     * 基于Redis的分布式锁实现
     */
    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnClass(name = "org.redisson.api.RedissonClient")
    public BaseZtLock getRedissonZtLock(){
        log.debug("【分布式锁】基于Redis的分布式锁实现加载...");
        return new RedissonZtLock();
    }

    /**
     * 加载分布式锁的切面
     * @return
     */
    @Bean
    @ConditionalOnBean(BaseZtLock.class)
    public ZtLockAspect getAspect(){
        log.debug("【分布式锁】分布式锁增强加载...");
        return new ZtLockAspect();
    }
}
